/**
 * Copyright (c) 2008, Andrew Carter All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer. Redistributions in binary
 * form must reproduce the above copyright notice, this list of conditions and
 * the following disclaimer in the documentation and/or other materials provided
 * with the distribution. Neither the name of Andrew Carter nor the names of
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package com.acarter.scenemonitor.propertydescriptor.definitions;

import com.acarter.propertytable.Property;
import com.acarter.propertytable.PropertySection;
import com.acarter.propertytable.PropertySectionState;
import com.acarter.propertytable.PropertyTable;
import com.acarter.propertytable.propertyobject.EnumPropertyObject;
import com.acarter.propertytable.propertyobject.FloatPropertyObject;
import com.acarter.propertytable.propertyobject.EnumPropertyObject.I_EnumPropertyObjectListener;
import com.acarter.propertytable.propertyobject.FloatPropertyObject.I_FloatPropertyObjectListener;
import com.acarter.scenemonitor.propertydescriptor.A_PropertyPage;
import com.acarter.scenemonitor.propertydescriptor.enums.E_TextureACF;
import com.acarter.scenemonitor.propertydescriptor.enums.E_TextureACO;
import com.acarter.scenemonitor.propertydescriptor.enums.E_TextureACS;
import com.acarter.scenemonitor.propertydescriptor.enums.E_TextureApplyMode;
import com.acarter.scenemonitor.propertydescriptor.enums.E_TextureEM;
import com.acarter.scenemonitor.propertydescriptor.enums.E_TextureMM;
import com.acarter.scenemonitor.propertydescriptor.enums.E_TextureRTT;
import com.acarter.scenemonitor.propertydescriptor.enums.E_TextureWM;
import com.acarter.scenemonitor.propertydescriptor.propertyobject.QuaternionPropertyObject;
import com.acarter.scenemonitor.propertydescriptor.propertyobject.Vector3fPropertyObject;
import com.acarter.scenemonitor.propertydescriptor.propertyobject.QuaternionPropertyObject.I_QuaternionPropertyObjectListener;
import com.acarter.scenemonitor.propertydescriptor.propertyobject.Vector3fPropertyObject.I_Vector3fPropertyObjectListener;
import com.jme.image.Texture;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;

/**
 * @author Carter
 * 
 */
public class TexturePropertyPage extends A_PropertyPage {

	/**  */
	private Texture texture = null;

	/**
	 * 
	 */
	public TexturePropertyPage() {
		
		PropertySection section = new PropertySection("Texture");
		section.addProperty(new Property("Anisotropic Filter Percent", new FloatPropertyObject()));
		section.addProperty(new Property("Apply Mode", new EnumPropertyObject<E_TextureApplyMode>()));
		section.addProperty(new Property("Environmental Map Mode", new EnumPropertyObject<E_TextureEM>()));
		section.addProperty(new Property("Mipmap Mode", new EnumPropertyObject<E_TextureMM>()));
		section.addProperty(new Property("Render To Texture Type", new EnumPropertyObject<E_TextureRTT>()));
		section.addProperty(new Property("Wrap Mode", new EnumPropertyObject<E_TextureWM>()));
		section.addProperty(new Property("Translation", new Vector3fPropertyObject()));
		section.addProperty(new Property("Rotation", new QuaternionPropertyObject()));
		section.addProperty(new Property("Scale", new Vector3fPropertyObject()));
		
		section.setState(PropertySectionState.EXPANDED);
		model.addPropertySection(section);

		section = new PropertySection("Alpha Combine");
		section.addProperty(new Property("Combiner Funtion", new EnumPropertyObject<E_TextureACF>()));
		section.addProperty(new Property("Combiner Source 0", new EnumPropertyObject<E_TextureACS>()));
		section.addProperty(new Property("Combiner Operand 0", new EnumPropertyObject<E_TextureACO>()));
		section.addProperty(new Property("Combiner Source 1", new EnumPropertyObject<E_TextureACS>()));
		section.addProperty(new Property("Combiner Operand 1", new EnumPropertyObject<E_TextureACO>()));
		section.addProperty(new Property("Combiner Source 2", new EnumPropertyObject<E_TextureACS>()));
		section.addProperty(new Property("Combiner Operand 2", new EnumPropertyObject<E_TextureACO>()));
		section.addProperty(new Property("Combiner Scale", new FloatPropertyObject()));
		
		section.setState(PropertySectionState.EXPANDED);
		model.addPropertySection(section);

		section = new PropertySection("RGB Combine");
		section.addProperty(new Property("Combiner Funtion", new EnumPropertyObject<E_TextureACF>()));
		section.addProperty(new Property("Combiner Source 0", new EnumPropertyObject<E_TextureACS>()));
		section.addProperty(new Property("Combiner Operand 0", new EnumPropertyObject<E_TextureACO>()));
		section.addProperty(new Property("Combiner Source 1", new EnumPropertyObject<E_TextureACS>()));
		section.addProperty(new Property("Combiner Operand 1", new EnumPropertyObject<E_TextureACO>()));
		section.addProperty(new Property("Combiner Source 2", new EnumPropertyObject<E_TextureACS>()));
		section.addProperty(new Property("Combiner Operand 2", new EnumPropertyObject<E_TextureACO>()));
		section.addProperty(new Property("Combiner Scale", new FloatPropertyObject()));
		
		section.setState(PropertySectionState.EXPANDED);
		model.addPropertySection(section);
	}

	/**
	 * @see com.acarter.scenemonitor.propertydescriptor.A_PropertyPage#updateInfo(com.acarter.propertytable.PropertyTableModel,
	 *      java.lang.Object)
	 */
	@Override
	public void updateModel(PropertyTable table, Object object) {

		if(object instanceof Texture) {

			Texture texture = (Texture) object;

			if(this.texture == null || !this.texture.equals(texture)) {

				this.texture = texture;
				updateListeners(texture);
			}
		}
	}

	/**
	 * 
	 * @param table
	 * @param object
	 */
	@SuppressWarnings("unchecked")
	protected void updateListeners(final Texture texture) {
		
		FloatPropertyObject aniso = (FloatPropertyObject)model.getPropertySection("Texture").getProperty("Anisotropic Filter Percent").getPropertyObject();
		
		aniso.SetListener(new I_FloatPropertyObjectListener() {

			public float readValue() {
				
				return texture.getAnisoLevel();
			}

			public void saveValue(float value) {
				
				texture.setAnisoLevel(value);
			}
		});
		
		EnumPropertyObject<E_TextureApplyMode> applyMode = (EnumPropertyObject<E_TextureApplyMode>)model.getPropertySection("Texture").getProperty("Apply Mode").getPropertyObject();
		
		applyMode.SetListener(new I_EnumPropertyObjectListener<E_TextureApplyMode>() {

			public E_TextureApplyMode readValue() {
				
				return E_TextureApplyMode.getConstant(texture.getApply());
			}

			public void saveValue(E_TextureApplyMode value) {
				
				texture.setApply(value.getType());
			}
		});
		
		EnumPropertyObject<E_TextureEM> environmentalMap = (EnumPropertyObject<E_TextureEM>)model.getPropertySection("Texture").getProperty("Environmental Map Mode").getPropertyObject();
		
		environmentalMap.SetListener(new I_EnumPropertyObjectListener<E_TextureEM>() {

			public E_TextureEM readValue() {
				
				return E_TextureEM.getConstant(texture.getEnvironmentalMapMode());
			}

			public void saveValue(E_TextureEM value) {
				
				texture.setEnvironmentalMapMode(value.getType());
			}
		});
		
		EnumPropertyObject<E_TextureMM> magnification = (EnumPropertyObject<E_TextureMM>)model.getPropertySection("Texture").getProperty("Mipmap Mode").getPropertyObject();
		
		magnification.SetListener(new I_EnumPropertyObjectListener<E_TextureMM>() {

			public E_TextureMM readValue() {
				
				return E_TextureMM.getConstant(texture.getMipmapState());
			}

			public void saveValue(E_TextureMM value) {
				
				texture.setMipmapState(value.getType());
			}
		});
		
		EnumPropertyObject<E_TextureRTT> rtt = (EnumPropertyObject<E_TextureRTT>)model.getPropertySection("Texture").getProperty("Render To Texture Type").getPropertyObject();
		
		rtt.SetListener(new I_EnumPropertyObjectListener<E_TextureRTT>() {

			public E_TextureRTT readValue() {
				
				return E_TextureRTT.getConstant(texture.getRTTSource());
			}

			public void saveValue(E_TextureRTT value) {
				
				texture.setRTTSource(value.getType());
			}
		});
		
		EnumPropertyObject<E_TextureWM> wrapS = (EnumPropertyObject<E_TextureWM>)model.getPropertySection("Texture").getProperty("Wrap Mode").getPropertyObject();
		
		wrapS.SetListener(new I_EnumPropertyObjectListener<E_TextureWM>() {

			public E_TextureWM readValue() {
				
				return E_TextureWM.getConstant(texture.getWrap());
			}

			public void saveValue(E_TextureWM value) {
	
				texture.setWrap(value.getType());
			}
		});
		
		Vector3fPropertyObject translation = (Vector3fPropertyObject)model.getPropertySection("Texture").getProperty("Translation").getPropertyObject();
		
		translation.SetListener(new I_Vector3fPropertyObjectListener() {

			public Vector3f readValue() {
				
				Vector3f v = texture.getTranslation();
				
				if(v == null)
					texture.setTranslation(new Vector3f());
				
				return texture.getTranslation();
			}

			public void saveValue(Vector3f value) {
	
				texture.setTranslation(value);
			}
		});
		
		QuaternionPropertyObject rotation = (QuaternionPropertyObject)model.getPropertySection("Texture").getProperty("Rotation").getPropertyObject();
		
		rotation.SetListener(new I_QuaternionPropertyObjectListener() {

			public Quaternion readValue() {
				
				Quaternion q = texture.getRotation();
				
				if(q == null)
					texture.setRotation(new Quaternion());
				
				return texture.getRotation();
			}

			public void saveValue(Quaternion value) {
	
				texture.setRotation(value);
			}
		});
		
		Vector3fPropertyObject scale = (Vector3fPropertyObject)model.getPropertySection("Texture").getProperty("Scale").getPropertyObject();
		
		scale.SetListener(new I_Vector3fPropertyObjectListener() {

			public Vector3f readValue() {
				
				Vector3f v = texture.getScale();
				
				if(v == null)
					texture.setScale(new Vector3f(1, 1, 1));
				
				return texture.getScale();
			}

			public void saveValue(Vector3f value) {
	
				texture.setScale(value);
			}
		});

		EnumPropertyObject<E_TextureACF> alphaFunction = (EnumPropertyObject<E_TextureACF>)model.getPropertySection("Alpha Combine").getProperty("Combiner Funtion").getPropertyObject();
		
		alphaFunction.SetListener(new I_EnumPropertyObjectListener<E_TextureACF>() {

			public E_TextureACF readValue() {
				
				return E_TextureACF.getConstant(texture.getCombineFuncAlpha());
			}

			public void saveValue(E_TextureACF value) {
	
				texture.setCombineFuncAlpha(value.getType());
			}
		});

		EnumPropertyObject<E_TextureACS> alphaSrc0 = (EnumPropertyObject<E_TextureACS>)model.getPropertySection("Alpha Combine").getProperty("Combiner Source 0").getPropertyObject();
		
		alphaSrc0.SetListener(new I_EnumPropertyObjectListener<E_TextureACS>() {

			public E_TextureACS readValue() {
				
				return E_TextureACS.getConstant(texture.getCombineSrc0Alpha());
			}

			public void saveValue(E_TextureACS value) {
	
				texture.setCombineSrc0Alpha(value.getType());
			}
		});

		EnumPropertyObject<E_TextureACO> alphaOp0 = (EnumPropertyObject<E_TextureACO>)model.getPropertySection("Alpha Combine").getProperty("Combiner Operand 0").getPropertyObject();
		
		alphaOp0.SetListener(new I_EnumPropertyObjectListener<E_TextureACO>() {

			public E_TextureACO readValue() {
				
				return E_TextureACO.getConstant(texture.getCombineOp0Alpha());
			}

			public void saveValue(E_TextureACO value) {
	
				texture.setCombineOp0Alpha(value.getType());
			}
		});

		EnumPropertyObject<E_TextureACS> alphaSrc1 = (EnumPropertyObject<E_TextureACS>)model.getPropertySection("Alpha Combine").getProperty("Combiner Source 1").getPropertyObject();
		
		alphaSrc1.SetListener(new I_EnumPropertyObjectListener<E_TextureACS>() {

			public E_TextureACS readValue() {
				
				return E_TextureACS.getConstant(texture.getCombineSrc1Alpha());
			}

			public void saveValue(E_TextureACS value) {
	
				texture.setCombineSrc1Alpha(value.getType());
			}
		});

		EnumPropertyObject<E_TextureACO> alphaOp1 = (EnumPropertyObject<E_TextureACO>)model.getPropertySection("Alpha Combine").getProperty("Combiner Operand 1").getPropertyObject();
		
		alphaOp1.SetListener(new I_EnumPropertyObjectListener<E_TextureACO>() {

			public E_TextureACO readValue() {
				
				return E_TextureACO.getConstant(texture.getCombineOp1Alpha());
			}

			public void saveValue(E_TextureACO value) {
	
				texture.setCombineOp1Alpha(value.getType());
			}
		});

		EnumPropertyObject<E_TextureACS> alphaSrc2 = (EnumPropertyObject<E_TextureACS>)model.getPropertySection("Alpha Combine").getProperty("Combiner Source 2").getPropertyObject();
		
		alphaSrc2.SetListener(new I_EnumPropertyObjectListener<E_TextureACS>() {

			public E_TextureACS readValue() {
				
				return E_TextureACS.getConstant(texture.getCombineSrc2Alpha());
			}

			public void saveValue(E_TextureACS value) {
	
				texture.setCombineSrc2Alpha(value.getType());
			}
		});

		EnumPropertyObject<E_TextureACO> alphaOp2 = (EnumPropertyObject<E_TextureACO>)model.getPropertySection("Alpha Combine").getProperty("Combiner Operand 2").getPropertyObject();
		
		alphaOp2.SetListener(new I_EnumPropertyObjectListener<E_TextureACO>() {

			public E_TextureACO readValue() {
				
				return E_TextureACO.getConstant(texture.getCombineOp2Alpha());
			}

			public void saveValue(E_TextureACO value) {
	
				texture.setCombineOp2Alpha(value.getType());
			}
		});
		
		FloatPropertyObject alphaScale = (FloatPropertyObject)model.getPropertySection("Alpha Combine").getProperty("Combiner Scale").getPropertyObject();
		
		alphaScale.SetListener(new I_FloatPropertyObjectListener() {

			public float readValue() {
				
				return texture.getCombineScaleAlpha();
			}

			public void saveValue(float value) {
				
				texture.setCombineScaleAlpha(value);
			}
		});

		EnumPropertyObject<E_TextureACF> rgbFunction = (EnumPropertyObject<E_TextureACF>)model.getPropertySection("RGB Combine").getProperty("Combiner Funtion").getPropertyObject();
		
		rgbFunction.SetListener(new I_EnumPropertyObjectListener<E_TextureACF>() {

			public E_TextureACF readValue() {
				
				return E_TextureACF.getConstant(texture.getCombineFuncRGB());
			}

			public void saveValue(E_TextureACF value) {
	
				texture.setCombineFuncRGB(value.getType());
			}
		});

		EnumPropertyObject<E_TextureACS> rgbSrc0 = (EnumPropertyObject<E_TextureACS>)model.getPropertySection("RGB Combine").getProperty("Combiner Source 0").getPropertyObject();
		
		rgbSrc0.SetListener(new I_EnumPropertyObjectListener<E_TextureACS>() {

			public E_TextureACS readValue() {
				
				return E_TextureACS.getConstant(texture.getCombineSrc0RGB());
			}

			public void saveValue(E_TextureACS value) {
	
				texture.setCombineSrc0RGB(value.getType());
			}
		});

		EnumPropertyObject<E_TextureACO> rgbOp0 = (EnumPropertyObject<E_TextureACO>)model.getPropertySection("RGB Combine").getProperty("Combiner Operand 0").getPropertyObject();
		
		rgbOp0.SetListener(new I_EnumPropertyObjectListener<E_TextureACO>() {

			public E_TextureACO readValue() {
				
				return E_TextureACO.getConstant(texture.getCombineOp0RGB());
			}

			public void saveValue(E_TextureACO value) {
	
				texture.setCombineOp0RGB(value.getType());
			}
		});

		EnumPropertyObject<E_TextureACS> rgbSrc1 = (EnumPropertyObject<E_TextureACS>)model.getPropertySection("RGB Combine").getProperty("Combiner Source 1").getPropertyObject();
		
		rgbSrc1.SetListener(new I_EnumPropertyObjectListener<E_TextureACS>() {

			public E_TextureACS readValue() {
				
				return E_TextureACS.getConstant(texture.getCombineSrc1RGB());
			}

			public void saveValue(E_TextureACS value) {
	
				texture.setCombineSrc1RGB(value.getType());
			}
		});

		EnumPropertyObject<E_TextureACO> rgbOp1 = (EnumPropertyObject<E_TextureACO>)model.getPropertySection("RGB Combine").getProperty("Combiner Operand 1").getPropertyObject();
		
		rgbOp1.SetListener(new I_EnumPropertyObjectListener<E_TextureACO>() {

			public E_TextureACO readValue() {
				
				return E_TextureACO.getConstant(texture.getCombineOp1RGB());
			}

			public void saveValue(E_TextureACO value) {
	
				texture.setCombineOp1RGB(value.getType());
			}
		});

		EnumPropertyObject<E_TextureACS> rgbSrc2 = (EnumPropertyObject<E_TextureACS>)model.getPropertySection("RGB Combine").getProperty("Combiner Source 2").getPropertyObject();
		
		rgbSrc2.SetListener(new I_EnumPropertyObjectListener<E_TextureACS>() {

			public E_TextureACS readValue() {
				
				return E_TextureACS.getConstant(texture.getCombineSrc2RGB());
			}

			public void saveValue(E_TextureACS value) {
	
				texture.setCombineSrc2RGB(value.getType());
			}
		});

		EnumPropertyObject<E_TextureACO> rgbOp2 = (EnumPropertyObject<E_TextureACO>)model.getPropertySection("RGB Combine").getProperty("Combiner Operand 2").getPropertyObject();
		
		rgbOp2.SetListener(new I_EnumPropertyObjectListener<E_TextureACO>() {

			public E_TextureACO readValue() {
				
				return E_TextureACO.getConstant(texture.getCombineOp2RGB());
			}

			public void saveValue(E_TextureACO value) {
	
				texture.setCombineOp2RGB(value.getType());
			}
		});
		
		FloatPropertyObject rgbScale = (FloatPropertyObject)model.getPropertySection("RGB Combine").getProperty("Combiner Scale").getPropertyObject();
		
		rgbScale.SetListener(new I_FloatPropertyObjectListener() {

			public float readValue() {
				
				return texture.getCombineScaleRGB();
			}

			public void saveValue(float value) {
				
				texture.setCombineScaleRGB(value);
			}
		});
	}
}
